<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Deo
 * Date: 10/26/13
 * Time: 12:22 AM
 * To change this template use File | Settings | File Templates.
 */

class FMIS_Meeting extends CI_Model{
    //exp_fmis_meeting

    public function getAllMeetings_Peek(){
        $sql = "
            SELECT exp_fmis_meeting.ID, MeetingName, exp_fmis_meeting_status.Description as 'Status', Date, Time, Location, concat(exp_fmis_member_profile.FirstName, ' ', exp_fmis_member_profile.LastName) as 'Recorder'
            FROM exp_fmis_meeting
            INNER JOIN exp_fmis_member_profile
            ON exp_fmis_meeting.RecorderID = exp_fmis_member_profile.MemberID
            INNER JOIN exp_fmis_meeting_status
            ON exp_fmis_meeting.Status = exp_fmis_meeting_status.ID
        ";

        return $this->db->query($sql)->result_array();
    }
    public function getAll()
    {
        $sql = "SELECT * FROM exp_fmis_meeting";
        /*
        $sql = "SELECT exp_fmis_meeting.ID, `MeetingName`, exp_fmis_member_profile.FirstName,
        `Agenda`, DATE(DateTime) as 'Date',
        DATE_FORMAT(TIME(DateTime), '%h:%i %p') as 'Time',`Location`, `Description`
        FROM `exp_fmis_meeting`INNER JOIN `exp_fmis_meeting_status` ON `Status` = exp_fmis_meeting_status.ID,
        `exp_fmis_member_profile` WHERE RecorderID = MemberID";
        */
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function addMeeting($dataArray)
    {
        $sql = "INSERT INTO exp_fmis_meeting(MeetingName,RecorderID, Agenda, Date, Time,
                  Location,Status)
                VALUES (?, ?, ?, ?, ?, ?, ?)";
        $this->db->query($sql, $dataArray);
    }

    public function editMeetingAgenda($dataArray)
    {
        $sql = "UPDATE exp_fmis_meeting SET Agenda = ? WHERE topic_id = ?";
        $query = $this->db->query($sql, $dataArray);
    }

    public function get_statusDesc()
    {
        $this->load->database();
        $sql = "Select * From exp_fmis_meeting_status";
        $query = $this->db->query($sql);

        return $query->result_array();
    }

    public function addMeetingAttendees($attendeeArray)
    {
        $meetingID = $this->getLastMeetingID();
        $meetingID = $meetingID->max_id;
        $ctr = 0;
        for($ctr = 0; $ctr<sizeof($attendeeArray); $ctr++){
            $sql = "INSERT INTO exp_fmis_meeting_member (MeetingID, MemberID) VALUES (?, ?)";
            $this->db->query($sql, array(
                $meetingID,
                $attendeeArray[$ctr]
            ));
        }
    }

    public function getLastMeetingID()
    {
        $sql = "SELECT MAX(ID) as max_id FROM exp_fmis_meeting";
        $query = $this->db->query($sql);
        return $query->row();
    }

    public function getMeetingDetails($id){
        $sql = "
            SELECT MeetingName, concat(LastName, ', ', FirstName) as 'Recorder', Agenda, Date, Time, Location
             FROM
             exp_fmis_meeting
             INNER JOIN exp_fmis_member_profile
             ON RecorderID = exp_fmis_member_profile.MemberID
             INNER JOIN exp_fmis_meeting_status
             ON exp_fmis_meeting_status.ID = exp_fmis_meeting.Status
             WHERE exp_fmis_meeting.id = ?
        ";
        $query = $this->db->query($sql, $id);
        return $query->row_array();
    }

    public function getMeetingAttendees($id)
    {
        $sql = "
            SELECT concat(LastName, ', ', FirstName) as 'Attendees'
             FROM exp_fmis_meeting_member
             INNER JOIN exp_fmis_member_profile
             ON exp_fmis_meeting_member.MemberID = exp_fmis_member_profile.MemberID
             WHERE MeetingID = ?
        ";
        $query = $this->db->query($sql, $id);
        return $query->result_array();
    }

    public function removeMeeting($id){
        $sql = "
            DELETE
            FROM exp_fmis_meeting
            WHERE ID = ?
        ";
        $this->db->query($sql, $id);
    }
}